package com.woniu.springbootrbac.service.impl;

import com.woniu.springbootrbac.dto.PerDto;
import com.woniu.springbootrbac.entity.SysPermission;
import com.woniu.springbootrbac.mapper.SysPermissionMapper;
import com.woniu.springbootrbac.service.PerService;
import com.woniu.springbootrbac.vo.PerVo;
import com.woniu.springbootrbac.vo.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class PerServiceImpl implements PerService {

    @Autowired
    private SysPermissionMapper mapper;
    @Override
    public List<PerVo> selectMenuByTel(String tel, Integer pid) {
        List<com.woniu.springbootrbac.vo.PerVo> perVos = mapper.selectPreVo(tel, pid);
        if (perVos != null && perVos.size()>0) {
            for (com.woniu.springbootrbac.vo.PerVo perVo : perVos) {
                List<com.woniu.springbootrbac.vo.PerVo> perVos1 = selectMenuByTel(tel, perVo.getId());
                perVo.setChildren(perVos1);
            }
        }
        return perVos;
    }

    @Override
    @Transactional
    public int insertPermission(PerDto perDto) {
        int i = mapper.deleteByRid(perDto.getRid());
        int i1 = mapper.insertPermission(perDto);
        return i1;
    }

    @Override
    public List<Integer> selectPerByRid(Integer rid) {
        return mapper.selectPerByRid(rid);
    }
    @Override
    public List<TreeNode> selectAllPer(Integer pid) {
        List<TreeNode> treeNodes = mapper.selectAllPer(pid);
        if (treeNodes != null) {
            for (TreeNode treeNode : treeNodes) {
                List<TreeNode> treeNodes1 = selectAllPer(treeNode.getId());
                treeNode.setChildren(treeNodes1);
            }
        }
        return treeNodes;
    }
}
